home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / xrulesde.sit / Xrules™ Tutorial / card_9184.txt < prev    next >
Encoding:
Text File  |  1991-06-24  |  8.2 KB  |  189 lines

  1. -- card: 9184 from stack: in
  2. -- bmap block id: 9336
  3. -- flags: 4000
  4. -- background id: 7050
  5. -- name: The Knowledge Base
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 01
  10. -- high flags: 0007
  11. -- rect: left=2 top=26 right=314 bottom=511
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 22
  16. -- text size: 10
  17. -- style flags: 0
  18. -- line height: 13
  19. -- part name: doc
  20.  
  21.  
  22. -- part contents for card part 1
  23. ----- text -----
  24. The knowledge base (KB) defines the ΓÇ£knowledgeΓÇ¥ of a rule based system, whether static or dynamic.  This knowledge is in the form of production rules and facts.  The extent of the knowledge in a system is called its domain. 
  25.  
  26. The rule base comprises the static knowledge of a rule based system.  This knowledge may be in the form of rules or facts and is input by the knowledge engineer.  This is the "code" of a rule based system.
  27.  
  28. The dynamic part of the knowledge base is sometimes known as "working memory".  It includes those facts established at run time and the state of the inference process.  The process for establishing these facts and the operation of the inference process will become clear in the tutorial.
  29.  
  30. Rules define conditions which, when met, lead to establishing facts or drawing conclusions.  They generally appear as "if-then" statements. If the conditions in the body of the rule are met, then the assertions or conclusions in the "then" part of the rule are made.  The individual conditions within the body of a rule are called ΓÇ£clausesΓÇ¥.  The rule "Corvette" below illustrates these components.  If the rule "Corvette" below proves true, it will assert the fact "model".
  31.  
  32.   rule Corvette                      -- rule ID
  33.   if 
  34.     fact make = Chevy and            -- clause
  35.     fact passengers = 2              -- clause
  36.   then 
  37.     put Corvette into fact model     -- conclusion (assert
  38.                                      -- fact "model")
  39.  
  40. Facts are ΓÇ£truthsΓÇ¥ or ΓÇ£knownsΓÇ¥ that are established or ΓÇ£assertedΓÇ¥ by the system, whether static or dynamic.  Facts may be asserted by a user's response to a question (dynamic), by the outcome of a rule (dynamic), or in the rule base 
  41. (static). 
  42.  
  43. In the following paragraphs, we will build a rule base for a simple rule based system.  We will use this rule base for the rest of the tutorial.
  44.  
  45. LetΓÇÖs say that we would like to establish a rule base for determining whether a vehicle on a lot was a Corvette, a Camaro, or a Mustang.  First of all, we must establish some rules that can differentiate among the three cars.  The make of the car can differentiate between a Mustang and a Corvette or a Camaro.  The number of passengers can differentiate between a Corvette and a Camaro.  The rules indicated below provide a start for our system.
  46.  
  47.   rule Corvette                        -- rule ID
  48.   if 
  49.     fact make = Chevy and              -- clause
  50.     fact passengers = 2                -- clause
  51.   then 
  52.     put Corvette into fact model       -- conclusion (assert
  53.                                        --    fact "model") 
  54.  
  55.   rule Camaro
  56.   if 
  57.     fact make = Chevy and
  58.     fact passengers = 4
  59.   then 
  60.     put Camaro into fact model
  61.  
  62.   rule Mustang
  63.   if 
  64.     fact make = Ford
  65.   then 
  66.     put Mustang into fact model
  67.  
  68. These rules look fine for distinguishing among the three models, but let's say that the person using the system doesn't know a car from a truck and the lot contains trucks and motorcycles in addition to the three models of cars.  In that case, we must establish rules that can identify the vehicle as a car.  The  rules below accomplish this, so we will add them the rule base.
  69.  
  70.   rule "car 1"
  71.   if 
  72.     fact wheels = 4 and
  73.     fact passengers <= 4 and
  74.     fact "used for cargo" = no
  75.   then 
  76.     put true into fact car
  77.  
  78.   rule "car 2"
  79.   if 
  80.     not (fact wheels = 4)
  81.   then 
  82.     put false into fact car
  83.  
  84.   rule "car 3"
  85.   if 
  86.     not (fact passengers <= 4)
  87.   then 
  88.     put false into fact car
  89.  
  90.   rule "car 4"
  91.   if 
  92.     not (fact "used for cargo" = no)
  93.   then 
  94.     put false into fact car
  95.  
  96. NOTE: We have to include rules containing the "not" of each clause in the rule
  97. "car 1" so that, during the inference process, the fact "car" is sure to be asserted either true or false.  The purists say that you shouldn't use ELSE or OR in a rule based system.   Otherwise, we could simply add the statement 
  98. "else put false into car" to rule "car 1".  Generally, in rule based systems, OR is accomplished by generating individual rules to establish each case of the OR.  In our example, rules "car 2", "car 3", and "car 4" represent the cases of an OR statement that can result in fact "car" being asserted "false".  Thus, these three rules are equivalent to the following statement:
  99.  
  100.   if
  101.     not (fact wheels = 4) or
  102.     not (fact passengers <= 4) or
  103.     not (fact "used for cargo" = no) or
  104.   then
  105.     put false into fact car
  106.  
  107. The reasons for making certain that facts get asserted will become clear later when we address the inference engine.
  108.  
  109. Now that we have rules to determine the fact "car", we need to incorporate this fact into the rules determining the car's model.  We do this by adding the clause "fact car" to each of the rules establishing "model" as seen below.
  110.  
  111.   rule Corvette
  112.   if 
  113.     fact car and
  114.     fact make = Chevy and
  115.     fact passengers = 2
  116.   then 
  117.     put Corvette into fact model
  118.  
  119.   rule Camaro
  120.   if 
  121.     fact car and
  122.     fact make = Chevy and
  123.     fact passengers = 4
  124.   then 
  125.     put Camaro into fact model
  126.  
  127.   rule Mustang
  128.   if 
  129.     fact car and
  130.     fact make = Ford
  131.   then 
  132.     put Mustang into fact model
  133.  
  134. The inference engine will use the rules "car 1", "car 2", "car 3", and "car 4" to assert fact "car" which will in turn be used to assert fact "model" through rules "Corvette", "Camaro", and "Mustang".
  135.  
  136. The seven rules above represent a simple rule base.  You will note that our 
  137. "knowledge" is severely limited.  Our domain in this case includes car models Corvette, Camaro, and Mustang.  Our rather ignorant "expert" system "knows" about nothing else (in fact, it only knows how to distinguish between the three models).  You will also notice that there is no control apparent in our rule base.  As you will see, the inference engine will determine the control.
  138.  
  139. Now that we have a sample rule base, we need a mechanism to tell the inference engine how and when to communicate with the user.  We will delve into the user interface only far enough to establish this mechanism.
  140.  
  141. First, we need a way to query the user about facts.  The facts that the user will have to specify include "make", "passengers", "wheels", and "used for 
  142. "cargo".  We don't need to ask about "model" or "car" because we have rules that will assert them.
  143.  
  144. We will use the keyword "FACT" to indicate a list of facts and their appropriate queries.  The inference engine will use that information to decide what prompt to display to the user when asking about a certain fact.  We need to add the fact declarations and their queries to the front of our rule base as shown below.
  145.  
  146.   fact
  147.     passengers 
  148.       "How many passengers can it carry?",
  149.     wheels 
  150.       "How many wheels does it have?",
  151.     "used for cargo" 
  152.        "Is it used to carry cargo?",
  153.     make             
  154.       "What is the make of the vehicle?"
  155.  
  156. We also need a way to inform the user that the system has reached a conclusion for rules "Corvette", "Camaro", and "Mustang" since they determine the fact 
  157. "model" (fact "model" is our "goal", or what we are trying to determine).  We will use the keyword "CONCLUDE" to do this.  So, we modify the three rules to reflect this change.
  158.  
  159.   rule Corvette
  160.   if 
  161.     fact car and
  162.     fact make = Chevy and
  163.     fact passengers = 2
  164.   then 
  165.     put Corvette into fact model
  166.     conclude "The car is a Corvette."
  167.  
  168.   rule Camaro
  169.   if 
  170.     fact car and
  171.     fact make = Chevy and
  172.     fact passengers = 4
  173.   then 
  174.     put Camaro into fact model
  175.     conclude "The car is a Camaro."
  176.  
  177.   rule Mustang
  178.   if 
  179.     fact car and
  180.     fact make = Ford
  181.   then 
  182.     put Mustang into fact model
  183.     conclude "The car is a Mustang."
  184.  
  185. We will be using this sample rule base throughout the rest of the tutorial.  The complete rule base appears at the end of this tutorial.
  186.  
  187. Now that we have this "knowledge" established, we need an inference engine to use it. The next section begins an examination of the inference engine.
  188.  
  189.